package com.focusai.efairy.ui.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.Toast;

import com.focusai.efairy.R;


/**
 * 时间区间选择器
 * Created by AlysaLee on 2016/5/9.
 */
public class DoubleDatePickerDialog extends AlertDialog implements DialogInterface.OnClickListener, DatePicker.OnDateChangedListener {
    private static final String START_YEAR = "start_year";
    private static final String END_YEAR = "end_year";
    private static final String START_MONTH = "start_month";
    private static final String END_MONTH = "end_month";
    private static final String START_DAY = "start_day";
    private static final String END_DAY = "end_day";

    private final DatePicker mDatePicker_start;
    private final DatePicker mDatePicker_end;
    private final OnDateSetListener mCallBack;


    public DoubleDatePickerDialog(Context context, OnDateSetListener callBack, int startYear, int startMonthOfYear, int startDayOfMonth) {
        this(context, 0, callBack, startYear, startMonthOfYear, startDayOfMonth, startYear, startMonthOfYear, startDayOfMonth);
    }

    public DoubleDatePickerDialog(Context context, int theme, OnDateSetListener callBack, int startYear, int startMonthOfYear, int startDayOfMonth, int endYear, int endMonthOfYear, int endDayOfMonth) {
        super(context, theme);
        mCallBack = callBack;
        setButton(BUTTON_POSITIVE, context.getString(R.string.sure), this);
        setButton(BUTTON_NEGATIVE, context.getString(R.string.cancal), this);
        setIcon(0);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.double_date_picker_dialog, null);
        setView(view);
        mDatePicker_start = (DatePicker) view.findViewById(R.id.dp_start_time);
        mDatePicker_end = (DatePicker) view.findViewById(R.id.dp_end_time);
        mDatePicker_start.init(startYear, startMonthOfYear, startDayOfMonth, this);
        mDatePicker_end.init(endYear, endMonthOfYear, endDayOfMonth, this);
        mDatePicker_start.setCalendarViewShown(false);
        mDatePicker_end.setCalendarViewShown(false);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
    }


    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case BUTTON_POSITIVE:
                String start = getDate(mDatePicker_start);
                String end = getDate(mDatePicker_end);
                if (start.compareTo(end) > 0) {
                    Toast.makeText(getContext(), "开始日期不能大于结束日期", Toast.LENGTH_SHORT).show();
                } else {
                    tryNotifyDateSet();
                }
                break;
        }
    }

    private String getDate(DatePicker datePicker) {
        StringBuilder sbTime = new StringBuilder();
        sbTime.append(datePicker.getYear());
        int startMonthOfYear = datePicker.getMonth();
        sbTime.append(++startMonthOfYear < 10 ? "0" + startMonthOfYear : startMonthOfYear);
        sbTime.append(datePicker.getDayOfMonth() > 9 ? datePicker.getDayOfMonth() : "0" + datePicker.getDayOfMonth());
        return sbTime.toString();

    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        if (view.getId() == R.id.dp_start_time)
            mDatePicker_start.init(year, month, day, this);
        if (view.getId() == R.id.dp_end_time)
            mDatePicker_end.init(year, month, day, this);
    }

    /**
     * 获得开始日期的DatePicker
     *
     * @return The calendar view.
     */
    public DatePicker getDatePickerStart() {
        return mDatePicker_start;
    }

    /**
     * 获得结束日期的DatePicker
     *
     * @return The calendar view.
     */
    public DatePicker getDatePickerEnd() {
        return mDatePicker_end;
    }

    /**
     * Sets the start date.
     *
     * @param year        The date year.
     * @param monthOfYear The date month.
     * @param dayOfMonth  The date day of month.
     */
    public void updateStartDate(int year, int monthOfYear, int dayOfMonth) {
        mDatePicker_start.updateDate(year, monthOfYear, dayOfMonth);
    }

    /**
     * Sets the end date.
     *
     * @param year        The date year.
     * @param monthOfYear The date month.
     * @param dayOfMonth  The date day of month.
     */
    public void updateEndDate(int year, int monthOfYear, int dayOfMonth) {
        mDatePicker_end.updateDate(year, monthOfYear, dayOfMonth);
    }

    private void tryNotifyDateSet() {
        if (mCallBack != null) {
            mDatePicker_start.clearFocus();
            mDatePicker_end.clearFocus();
            mCallBack.onDateSet(mDatePicker_start, mDatePicker_start.getYear(), mDatePicker_start.getMonth(),
                    mDatePicker_start.getDayOfMonth(), mDatePicker_end, mDatePicker_end.getYear(),
                    mDatePicker_end.getMonth(), mDatePicker_end.getDayOfMonth());
        }
    }

    @Override
    public Bundle onSaveInstanceState() {
        Bundle state = super.onSaveInstanceState();
        state.putInt(START_YEAR, mDatePicker_start.getYear());
        state.putInt(START_MONTH, mDatePicker_start.getMonth());
        state.putInt(START_DAY, mDatePicker_start.getDayOfMonth());
        state.putInt(END_YEAR, mDatePicker_end.getYear());
        state.putInt(END_MONTH, mDatePicker_end.getMonth());
        state.putInt(END_DAY, mDatePicker_end.getDayOfMonth());
        return state;
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        int start_year = savedInstanceState.getInt(START_YEAR);
        int start_month = savedInstanceState.getInt(START_MONTH);
        int start_day = savedInstanceState.getInt(START_DAY);
        mDatePicker_start.init(start_year, start_month, start_day, this);

        int end_year = savedInstanceState.getInt(END_YEAR);
        int end_month = savedInstanceState.getInt(END_MONTH);
        int end_day = savedInstanceState.getInt(END_DAY);
        mDatePicker_end.init(end_year, end_month, end_day, this);

    }

    public interface OnDateSetListener {
        void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth,
                       DatePicker endDatePicker, int endYear, int endMonthOfYear, int endDayOfMonth);
    }
}
